package com.rainkaze.lifeharbor.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "LifeHarborDB.db";
    private static final int DATABASE_VERSION = 1;

    public static final String TABLE_FAVORITE_TOOLS = "favorite_tools";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_TOOL_IDENTIFIER = "tool_identifier";
    public static final String COLUMN_IS_FAVORITE = "is_favorite"; // 1是收藏, 0是没有收藏

    private static final String TABLE_CREATE_FAVORITE_TOOLS =
            "CREATE TABLE " + TABLE_FAVORITE_TOOLS + " (" +
                    COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_TOOL_IDENTIFIER + " TEXT UNIQUE NOT NULL, " +
                    COLUMN_IS_FAVORITE + " INTEGER NOT NULL DEFAULT 0" +
                    ");";

    public DatabaseHelper(@Nullable Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLE_CREATE_FAVORITE_TOOLS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITE_TOOLS);
        onCreate(db);
    }

    /**
     * 设置或更新工具的收藏状态
     * @param toolIdentifier 工具的唯一标识
     * @param isFavorite 是否收藏
     */
    public void setToolFavoriteStatus(String toolIdentifier, boolean isFavorite) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_TOOL_IDENTIFIER, toolIdentifier);
        values.put(COLUMN_IS_FAVORITE, isFavorite ? 1 : 0);

        // 尝试更新，如果不存在则插入
        // 使用 insertWithOnConflict 简化操作，如果 tool_identifier 冲突则替换
        db.insertWithOnConflict(TABLE_FAVORITE_TOOLS, null, values, SQLiteDatabase.CONFLICT_REPLACE);
        db.close();
    }

    /**
     * 获取工具的收藏状态
     * @param toolIdentifier 工具的唯一标识
     * @return true 如果已收藏, false 如果未收藏或不存在记录
     */
    public boolean isToolFavorite(String toolIdentifier) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = null;
        boolean isFavorite = false;
        try {
            cursor = db.query(TABLE_FAVORITE_TOOLS,
                    new String[]{COLUMN_IS_FAVORITE},
                    COLUMN_TOOL_IDENTIFIER + " = ?",
                    new String[]{toolIdentifier},
                    null, null, null);

            if (cursor != null && cursor.moveToFirst()) {
                isFavorite = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_IS_FAVORITE)) == 1;
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            db.close();
        }
        return isFavorite;
    }
}